package com.baozun.houji.order.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.baozhun.mall.common.arouter.RoutePath
import com.baozhun.mall.common.base.BaseActivity
import com.baozhun.mall.common.ext.showToast
import com.baozhun.mall.common.util.Constants
import com.baozhun.mall.common.widget.dialog.CommonChooseDialog
import com.baozun.houji.order.model.enums.RefundType
import com.baozun.houji.order.viewmodel.ApplyRefundViewModel
import com.baozun.houji.order.R
import com.baozun.houji.order.databinding.ActivityApplyRefundBinding


/**
 * @Title：申请售后
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/20
 * @Version：1.0.0
 */
@Route(path = RoutePath.Order.APPLY_REFUND)
class ApplyRefundActivity : BaseActivity<ApplyRefundViewModel, ActivityApplyRefundBinding>() {

    private val mOrderId by lazy { intent.getStringExtra(Constants.Order.ORDER_ID).toString() }
    private val mIsOnlyReturnMoney by lazy { intent.getBooleanExtra(Constants.Order.ONLY_RETURN_MONEY, false) }

    companion object {
        fun start(context: Context, orderId: String?, isOnlyReturnMoney: Boolean) {
            var intent = Intent(context, ApplyRefundActivity::class.java)
            intent.putExtra(Constants.Order.ORDER_ID, orderId)
            intent.putExtra(Constants.Order.ONLY_RETURN_MONEY, isOnlyReturnMoney)
            context.startActivity(intent)
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.getRefundInfo(mOrderId)
        mViewModel.isOnlyReturnMoney.value = mIsOnlyReturnMoney
        if (mIsOnlyReturnMoney) {
            mViewModel.mRefundType.value = RefundType.REFUND
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.apply_service_layout -> { //申请類型
                if (mIsOnlyReturnMoney) return
                CommonChooseDialog(
                    this,
                    com.baozhun.mall.res.R.string.please_choose_refund_type,
                    mViewModel.mRefundType.value?.value,
                    mutableListOf(RefundType.REFUND.value, RefundType.REFUND_AND_RETURN_GOODS.value)
                ) { _: String, selectPosition: Int ->
                    var selectType = if (selectPosition == 0) RefundType.REFUND else RefundType.REFUND_AND_RETURN_GOODS
                    //选择的售后类型不一样清除之前的选择的售后原因
                    if (selectType.type != mViewModel.mRefundType.value?.type) {
                        mViewModel.mRefundReason.value = null
                    }
                    mViewModel.mRefundType.value = selectType

                }.show()
            }
            R.id.apply_service_reason_layout -> { //申请原因
                if (mViewModel.mRefundType.value == null) {
                    showToast(com.baozhun.mall.res.R.string.please_choose_apply_service)
                    return
                }
                mViewModel.getRefundReason(mViewModel.mRefundType.value!!.type) {
                    CommonChooseDialog(
                        this,
                        com.baozhun.mall.res.R.string.please_choose_refund_reason,
                        mViewModel.mRefundReason.value?.reason,
                        it.map { it.reason }.toMutableList() as MutableList<String>
                    ) { _: String, selectPosition: Int ->
                        mViewModel.mRefundReason.value = it[selectPosition]
                    }.show()
                }
            }
            R.id.submit_apply_tv -> {
                mViewModel.submit(mOrderId) {
                    RefundDetailActivity.start(this, it)
                    finish()
                }
            }
        }
    }
}